package nio.security;

import java.security.Provider;
import java.security.Provider.Service;
import java.security.Security;
import java.util.Map.Entry;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * ProviderHelper
 * 
 * @author nio
 */
public class ProviderHelper extends Object {
	private static final Logger log = LoggerFactory.getLogger(ProviderHelper.class);

	/**
	 * Create a new instance of the ProviderHelper.
	 */
	public ProviderHelper() {
		super();
	}

	public void list() {
		Provider[] providers = Security.getProviders();
		StringBuffer stringBuffer = new StringBuffer();

		for (Provider provider : providers) {
			stringBuffer.delete(0, stringBuffer.length());
			String name = provider.getName();
			String info = provider.getInfo();
			double version = provider.getVersion();
			Class<?> clazz = provider.getClass();

			stringBuffer.append("--------------------------------------------------------------------------------\n");
			stringBuffer.append("Provider\n");
			stringBuffer.append("--------------------------------------------------------------------------------\n");
			stringBuffer.append("Name=" + name + "\n");
			stringBuffer.append("Version=" + version + "\n");
			stringBuffer.append("Class=" + clazz + "\n");
			stringBuffer.append("Info=" + info + "\n");

			stringBuffer.append("\nProperties\n");
			stringBuffer.append("--------------------------------------------------------------------------------\n");
			for (Entry<Object, Object> entry : provider.entrySet()) {
				Object key = entry.getKey();
				Object value = entry.getValue();
				stringBuffer.append("Key=" + key + "\n");
				stringBuffer.append("Value=" + value + "\n");
			}

			stringBuffer.append("\nServices\n");
			stringBuffer.append("--------------------------------------------------------------------------------\n");
			for (Service service : provider.getServices()) {
				String algorithm = service.getAlgorithm();
				String type = service.getType();
				stringBuffer.append("Type=" + type + "\n");
				stringBuffer.append("Algorithm=" + algorithm + "\n");
			}

			stringBuffer.append("--------------------------------------------------------------------------------");
			stringBuffer.trimToSize();
			
			System.out.println(stringBuffer.toString());
			
			log.info(stringBuffer.toString());
		}

		// log.error(e.getMessage(), e);

	}

}
